#!/usr/bin/env python

#################################################################################
#
# Copyright (c) 2006 Michigan State University
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#################################################################################

#################################################################################
#
#	Author:
#		Nathan Collins <npcollins@gmail.com>
#
#################################################################################

from querymgr import *
import sys

def createRepTable():
	# query to delete the old table
	dropQuery = "DROP TABLE IF EXISTS harvestRepManager;"
	# query to recreate the table
	createQuery = """
		CREATE TABLE harvestRepManager (
		recID INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
		repositUrl VARCHAR(255) NOT NULL UNIQUE KEY,
		lastHarvest DATETIME
		)
		CHARACTER SET utf8;
		"""

	dbquery(dropQuery)
	dbquery(createQuery)

def addRepTable(url):
	# query to add repository
	insertdict = {}
	insertdict['repositUrl'] = url
	insertdict['lastHarvest'] = '20000101000000'

	try:
		dbinsertsafe('harvestRepManager', insertDict)
	except:
		print "Could not add repository. (This could just mean it's a duplicate.)"


if __name__ == '__main__':
	argc = len(sys.argv)
	if argc < 2 or (sys.argv[1] == 'reset' and argc > 2):
		print """
usage: repmgr.py [reset|add] [url]

	reset 	   - this will delete and recreate the table containing the repositories
		     Note: Once recreated the old repository list is UNRECOVERABLE.
	add [url]  - this will add 'url' to the list of repositories to harvest
	[url] 	   - this is the same as 'add [url]

		"""
		sys.exit(0)
	if sys.argv[1] == 'reset':
		print 'Reseting table...'
		createRepTable()
	else:
		print 'Adding %s...' % sys.argv[1]
		addRepTable(sys.argv[1])

# eof

